;=================================================================================
;=========== SOME FUNCTION FOR VECTEURS 3D
;=================================================================================
Structure iVECTOR2
  x.f
  y.f
EndStructure

Macro VECTOR2_(dest,v1,v2)
  dest\x = v1
  dest\y = v2
EndMacro

Procedure.l Vec2_Equals(*vec1.iVECTOR2, *vec2.iVECTOR2)
  *vec1\x = *vec2\x
  *vec1\y = *vec2\y
EndProcedure

Procedure.l Vec2_isEquals(*vec1.iVECTOR2, *vec2.iVECTOR2)
  If (*vec1\x = *vec2\x) And (*vec1\y = *vec2\y)
    ProcedureReturn 1
  EndIf
  ProcedureReturn 0
EndProcedure

Procedure Vec2_Add(*res.iVECTOR2, *vec1.iVECTOR2, *vec2.iVECTOR2)
  *res\x = *vec1\x + *vec2\x
  *res\y = *vec1\y + *vec2\y
EndProcedure

Procedure Vec2_Sub(*res.iVECTOR2, *vec1.iVECTOR2, *vec2.iVECTOR2)
  *res\x = *vec1\x - *vec2\x
  *res\y = *vec1\y - *vec2\y
EndProcedure

Procedure Vec2_Mul(*res.iVECTOR2, *vec1.iVECTOR2, *vec2.iVECTOR2)
  *res\x = *vec1\x * *vec2\x
  *res\y = *vec1\y * *vec2\y
EndProcedure

Procedure Vec2_MulF(*res.iVECTOR2, *vec1.iVECTOR2, length.f)
  *res\x = *vec1\x * length
  *res\y = *vec1\y * length
EndProcedure

Procedure.f Vec2_DotProduct(*vec1.iVECTOR2, *vec2.iVECTOR2)
  Protected l.f
  l = *vec1\x * *vec2\x + *vec1\y * *vec2\y 
  ProcedureReturn l
EndProcedure

Procedure.f Vec2_GetLength(*vec1.iVECTOR2)
  Protected res.f
    res = (*vec1\x * *vec1\x) + (*vec1\y * *vec1\y) 
    ProcedureReturn Sqr(res)
EndProcedure

Procedure.f Vec2_GetDistanceFrom(*vec1.iVECTOR2, *vec2.iVECTOR2)
    Protected tmp.iVECTOR2
    tmp\x = *vec1\x - *vec2\x
    tmp\y = *vec1\y - *vec2\y
    res.f = Vec2_GetLength( @tmp )
    ProcedureReturn res
EndProcedure


 ; SELF

Procedure Vec2_Normalize(*vec1.iVECTOR2)
    Protected length.f
    length = (*vec1\x * *vec1\x) + (*vec1\y * *vec1\y) 
    If length=0 : ProcedureReturn 0 : EndIf
    length = 1.0 / Sqr( lenght )
    *vec1\x = *vec1\x * length
    *vec1\y = *vec1\y * length
EndProcedure
 
Procedure Vec2_SetLength(*vec1.iVECTOR2, newLength.f)
    Vec2_Normalize(*vec1)
    Vec2_MulF(*vec1, *vec1, newLength)
EndProcedure

Procedure Vec2_Invert(*vec1.iVECTOR2)
    *vec1\x = *vec1\x * -1.0
    *vec1\y = *vec1\y * -1.0
EndProcedure

Procedure Vec2_Rotate(angle.f, *vec.iVECTOR2, *center.iVECTOR2)
   Protected cs.f, sn.f
   cs = Cos(angle)
   sn = Sin(angle)
   *vec\x = *vec\x - *center\x
   *vec\y = *vec\y - *center\y
   *vec\x = ( *vec\x * cs - *vec\y*sn ) 
   *vec\y = ( *vec\x * sn + *vec\y*cs ) 
   *vec\x = *vec\x + *center\x
   *vec\y = *vec\y + *center\y
EndProcedure



; IDE Options = PureBasic 4.30 Beta 5 (Windows - x86)
; CursorPosition = 3
; Folding = ---